package io.renren.modules.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.ExcelUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.task.dao.MtCcEquipmentRunInfoLogDao;
import io.renren.modules.task.entity.MtCcEquipmentRunInfoLogEntity;
import io.renren.modules.task.model.MtCcEquipmentRunInfoLogVo;
import io.renren.modules.task.service.MtCcEquipmentRunInfoLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("mtCcEquipmentRunInfoLogService")
public class MtCcEquipmentRunInfoLogServiceImpl extends ServiceImpl<MtCcEquipmentRunInfoLogDao, MtCcEquipmentRunInfoLogEntity> implements MtCcEquipmentRunInfoLogService {

    @Autowired
    private MtCcEquipmentRunInfoLogDao mtCcEquipmentRunInfoLogDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MtCcEquipmentRunInfoLogEntity> page = this.page(
                new Query<MtCcEquipmentRunInfoLogEntity>().getPage(params),
                new QueryWrapper<MtCcEquipmentRunInfoLogEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByTerm(Map<String, Object> params) {
        Page<MtCcEquipmentRunInfoLogVo> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));
        List<MtCcEquipmentRunInfoLogVo> list = mtCcEquipmentRunInfoLogDao.queryPageByTerm(page,params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public Integer selectCountByTaskType(Integer taskType) {
        return mtCcEquipmentRunInfoLogDao.selectCountByTaskType(taskType);
    }

    @Override
    public String consoleLogToExcel(HttpServletResponse response,Map<String, Object> params) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String fileName = sdf.format(new Date());
//            String fileName="项目审核表";
            List<MtCcEquipmentRunInfoLogVo> listPcd = mtCcEquipmentRunInfoLogDao.queryRunInfoLogByParams(params);
            List<Map<String,Object>> list = createExcelRecord(listPcd);
            String columnNames[] = {"操作台名字","任务类型","添加时间","操作人"};//列名
            String keys[] = {"consoleName","taskType","createTime","userName"};//map中的key
            ExcelUtil.downloadWorkBook(list,keys,columnNames,fileName,response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "excel";
    }

    @Override
    public List<MtCcEquipmentRunInfoLogEntity> selectRunInfoLogByConsole(Map<String, Object> params) {
        return mtCcEquipmentRunInfoLogDao.selectRunInfoLogByConsole(params);
    }

    /**
     * 创建Excel表中的记录
     * @param pcdList
     * @return
     */
    private List<Map<String, Object>> createExcelRecord(List<MtCcEquipmentRunInfoLogVo> pcdList){
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("sheetName", "sheet1");
            listmap.add(map);
            for (int j = 0; j < pcdList.size(); j++) {
                MtCcEquipmentRunInfoLogVo mtCcEquipmentRunInfoLogVo = pcdList.get(j);
                Map<String, Object> mapValue = new HashMap<String, Object>();
                mapValue.put("consoleName",mtCcEquipmentRunInfoLogVo.getConsoleName());
                mapValue.put("taskType",mtCcEquipmentRunInfoLogVo.getTaskName());
                mapValue.put("createTime",sdf.format(mtCcEquipmentRunInfoLogVo.getCreateTime()));
                mapValue.put("userName",mtCcEquipmentRunInfoLogVo.getUserName());
                //mapValue.put("submitTime", DateTimeUtil.dateToStr(projectAuditListVo.getSubmitTime(),"yyyy-MM-dd"));
                //String attachmentURL = projectAuditListVo.getAttachment()==null?"无": FileUtil.getUploadPath()+projectAuditListVo.getAttachment();

                listmap.add(mapValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listmap;
    }


}